import logging
from django.core.management.base import BaseCommand
from recommendation.services import DeepSeekRecommendationService

logger = logging.getLogger('recommendation')

class Command(BaseCommand):
    help = '生成用户的盲盒推荐'
    
    def add_arguments(self, parser):
        parser.add_argument(
            '--user-id',
            type=int,
            help='特定用户ID，如果不提供则为所有活跃用户生成推荐'
        )
        parser.add_argument(
            '--limit',
            type=int,
            default=8,
            help='为每个用户生成的推荐数量'
        )
    
    def handle(self, *args, **options):
        user_id = options.get('user_id')
        limit = options.get('limit')
        
        self.stdout.write(self.style.SUCCESS('开始生成推荐...'))
        
        try:
            service = DeepSeekRecommendationService()
            
            # 调用推荐服务生成推荐
            recommendations_generated = service.generate_user_recommendations(user_id=user_id, limit=limit)
            
            self.stdout.write(self.style.SUCCESS(
                f"推荐生成成功！生成了 {recommendations_generated} 个用户的推荐。"
                f"每个用户最多 {limit} 条推荐。"
            ))
        
        except Exception as e:
            logger.exception("生成推荐时出错")
            self.stdout.write(self.style.ERROR(f"生成推荐时出错: {str(e)}")) 